package com.prft.rms.jsf;

import java.util.Date;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.prft.rms.entity.Employee;
import com.prft.rms.service.EmployeeService;

public class EmployeeBean {

	Logger logger = LoggerFactory.getLogger(EmployeeBean.class);
	private boolean draggableRendered = false;
	private boolean modalRendered = false;
	private boolean autoCentre = true;
	private Integer emid;
	private EmployeeService employeeService;
	private Employee employee;

	public void toggleModal(ActionEvent event) {
		modalRendered = !modalRendered;
		UIComponent component = event.getComponent();
		Map<String, Object> map = component.getAttributes();
		emid = (Integer) map.get("emid");
		logger.debug("emid is " + emid);
		if (modalRendered) {
			loadEmpoyee();
			System.out.println(employee.getDegree());
			System.out.println(employee.getChineseName());
		}
	}

	public void loadEmpoyee() {
		if (emid != null) {
			logger.debug("service load begin");
			employee = employeeService.getEmployee(emid);
			logger.debug("service load end");
		}
	}

	public void updateEmployee(ActionEvent event) {
		logger.debug("updated Employee emid: " + employee.getEmid());
		logger.debug("updated" + employee.getScreenName());
		employee.setLastUpdated(new Date());
		employeeService.saveEmployee(employee);
		modalRendered = false;
	}

	public boolean isDraggableRendered() {
		return draggableRendered;
	}

	public void setDraggableRendered(boolean draggableRendered) {
		this.draggableRendered = draggableRendered;
	}

	public boolean isModalRendered() {
		logger.debug("modalRendered: " + modalRendered);
		return modalRendered;
	}

	public void setModalRendered(boolean modalRendered) {
		this.modalRendered = modalRendered;
	}

	public boolean isAutoCentre() {
		return autoCentre;
	}

	public void setAutoCentre(boolean autoCentre) {
		this.autoCentre = autoCentre;
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public Integer getEmid() {
		return emid;
	}

	public void setEmid(Integer emid) {
		logger.debug("set emid " + emid);
		this.emid = emid;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
}
